// pages/consultation-online/index.js
const api = require("./../../config/api")
const request = require("./../../utils/request")
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		getTypeList: '',
		QAlist: [],
		tilast: '',
		height: ''
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.setData({
			height: wx.getSystemInfoSync().windowHeight
		})
		this.getList()
	},
	//获取咨询分类列表
	getList: async function() {
		const res = await request.get(api.getTypeList)
		if (res.httpStatus == 200) {
			this.setData({
				getTypeList: res.data
			})
		}
	},
	// 获取分类关键字
	getTypeItem: async function(val) {
		const item = val.currentTarget.dataset.item
		const res = await request.get(api.getKeyword, {
			typeId: item.id
		})
		let newList = this.data.QAlist
		if (res.httpStatus === 200) {
			let list = {
				keyWordList: [],
				answer: {},
				title: item.dictKey,
				getTypeList: this.data.getTypeList
			}
			list.keyWordList = res.data
			newList.push(list)
			this.setData({
				QAlist: newList,
				tilast: `item${newList.length - 1}`
			})
		}
	},
	// 获取答案
	getAnswer: async function(val) {
		const idx = val.currentTarget.dataset.idx
		const index = val.currentTarget.dataset.index
		const item = val.currentTarget.dataset.item
		const fitem = val.currentTarget.dataset.fitem
		const res = await request.get(api.getKeywordById, {
			consultId: item.id
		})
		const newlist = this.data.QAlist
		if (res.httpStatus === 200) {
			newlist[idx].answer = res.data
			let list = {
				keyWordList: newlist[newlist.length - 1].keyWordList,
				answer: {},
				title: newlist[newlist.length - 1].title,
				getTypeList: this.data.getTypeList
			}
			newlist.push(list)
			this.setData({
				QAlist: newlist,
				tilast: `item${newlist.length - 1}`
			})
		}
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {
		wx.switchTab({
			url: '/pages/index/index'
		})
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
